package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.CusDeviceSpareDTO;
import com.xinsoft.entity.dto.CusDeviceSpareDetailDTO;
import com.xinsoft.entity.dto.CusDeviceSparePageParam;
import com.xinsoft.entity.po.CusDeviceSpare;
import com.xinsoft.entity.vo.CusDeviceSpareVo;
import com.xinsoft.mapper.CusDeviceSpareMapper;
import com.xinsoft.service.CusDeviceSpareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author hsm
* @description 针对表【cus_device_spare(设备备件)】的数据库操作Service实现
* @createDate 2023-06-16 13:16:47
*/
@Service
public class CusDeviceSpareServiceImpl extends ServiceImpl<CusDeviceSpareMapper, CusDeviceSpare>
    implements CusDeviceSpareService{

//    @Override
//    public void addCusDeviceSpareList(CusDeviceSpareDTO cusDeviceSpareDTO) {
//        //新增时判断是否重复关联，已经存在的则不关联
//        if (ObjectUtil.isEmpty(cusDeviceSpareDTO)) return;
//        List<CusDeviceSpareDetailDTO> detailDTOS = cusDeviceSpareDTO.getDetailDTOS();//全部配件
//        Map<Integer, CusDeviceSpareDetailDTO> detailDTOMap = detailDTOS.stream().collect(Collectors.toMap(item -> item.getMaterialId(), item -> item, (v1, v2) -> v1));
//        List<CusDeviceSpare> spareDOS = this.list(new LambdaQueryWrapper<CusDeviceSpare>()
//                .eq(CusDeviceSpare::getDeviceId, cusDeviceSpareDTO.getDeviceId()));//已有配件
//        if (ObjectUtil.isNotEmpty(spareDOS)) {
//            spareDOS.forEach(item -> {
//                if (detailDTOMap.containsKey(item.getMaterialId())) {
//                    CusDeviceSpareDetailDTO updateDto = detailDTOMap.get(item.getMaterialId());
//                    updateDto.setId(item.getId());
//                }
//            });
//        }
//        Collection<CusDeviceSpareDetailDTO> addDetailCollection = detailDTOMap.values();//新增配件
//        List<CusDeviceSpareDetailDTO> addDetailDTOS = addDetailCollection.stream().collect(Collectors.toList());
//        if (ObjectUtil.isEmpty(addDetailDTOS)) return;
//        List<CusDeviceSpare> addDetailDOS = addDetailDTOS.stream().map(item -> {
//            CusDeviceSpare cusDeviceSpare = new CusDeviceSpare();
//            cusDeviceSpare.setId(item.getId());
//            cusDeviceSpare.setMaterialId(item.getMaterialId());
//            cusDeviceSpare.setMixStockNum(item.getMixStockNum());
//            cusDeviceSpare.setDeviceId(cusDeviceSpareDTO.getDeviceId());
//            return cusDeviceSpare;
//        }).collect(Collectors.toList());
//        this.saveOrUpdateBatch(addDetailDOS);
//    }

    @Override
    public void addCusDeviceSpareList(CusDeviceSpareDTO cusDeviceSpareDTO) {
        //新增时判断是否重复关联，已经存在的则不关联
        if(CollectionUtil.isNotEmpty(cusDeviceSpareDTO.getMaterialIds())){

            for (Integer materialId : cusDeviceSpareDTO.getMaterialIds()) {

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("material_id",materialId);
                queryWrapper.eq("device_id",cusDeviceSpareDTO.getDeviceId());
                List list =list(queryWrapper);
                if(CollectionUtil.isNotEmpty(list)){
                    continue;
                }
                CusDeviceSpare cusDeviceSpare = new CusDeviceSpare();
                cusDeviceSpare.setMaterialId(materialId);
                cusDeviceSpare.setDeviceId(cusDeviceSpareDTO.getDeviceId());
                save(cusDeviceSpare);
            }
        }
    }


    @Override
    public void del(List<Integer> ids) {

        removeByIds(ids);

    }

    @Override
    public IPage<CusDeviceSpareVo> findPageListByParam(CusDeviceSparePageParam cusDevicePageParam) {
        //分页
        Page<CusDeviceSpareVo> page = new Page<>(cusDevicePageParam.getCurrentPage(), cusDevicePageParam.getPageSize());
        //查询分页
        IPage<CusDeviceSpareVo> pageList = this.baseMapper.findListByParam(page, cusDevicePageParam);

        return pageList;
    }
}




